<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Android自定义View-02 视图动画 | 码行</title><meta name="author" content="Carl Su"><meta name="copyright" content="Carl Su"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="动画类型View Animation(视图动画) Tween Animation(补间动画) Frame Animation(逐帧动画)  Property Animation(属性动画) ValueAnimator ObjectAnimator  一. 视图动画标签1.概述Android动画由5种类型组成：alpha、scale、translate、rotate、set 1.1配置XML动画文件">
<meta property="og:type" content="article">
<meta property="og:title" content="Android自定义View-02 视图动画">
<meta property="og:url" content="https://devsu.top/posts/d2fda48f/index.html">
<meta property="og:site_name" content="码行">
<meta property="og:description" content="动画类型View Animation(视图动画) Tween Animation(补间动画) Frame Animation(逐帧动画)  Property Animation(属性动画) ValueAnimator ObjectAnimator  一. 视图动画标签1.概述Android动画由5种类型组成：alpha、scale、translate、rotate、set 1.1配置XML动画文件">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2023/03/14/RLM1qPGF3S6sNnJ.png">
<meta property="article:published_time" content="2020-08-05T02:59:14.000Z">
<meta property="article:modified_time" content="2023-03-28T08:50:54.318Z">
<meta property="article:author" content="Carl Su">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/03/14/RLM1qPGF3S6sNnJ.png"><link rel="shortcut icon" href="https://s2.loli.net/2023/03/09/whMHdYkrKoIBnSC.png"><link rel="canonical" href="https://devsu.top/posts/d2fda48f/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6243425e2ecb037931274c4ca1db3132";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Carl Su","link":"链接: ","source":"来源: 码行","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Android自定义View-02 视图动画',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-28 16:50:54'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2023/03/09/QrI9mgJ4XlbEPzN.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">169</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">37</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('https://s2.loli.net/2023/03/14/RLM1qPGF3S6sNnJ.png')"><nav id="nav"><span id="blog-info"><a href="/" title="码行"><img class="site-icon" src="https://s2.loli.net/2023/03/09/whMHdYkrKoIBnSC.png"/><span class="site-name">码行</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Android自定义View-02 视图动画</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-08-05T02:59:14.000Z" title="发表于 2020-08-05 10:59:14">2020-08-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-28T08:50:54.318Z" title="更新于 2023-03-28 16:50:54">2023-03-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Android/">Android</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Android/%E8%87%AA%E5%AE%9A%E4%B9%89View/">自定义View</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>16分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Android自定义View-02 视图动画"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="动画类型"><a href="#动画类型" class="headerlink" title="动画类型"></a>动画类型</h2><h3 id="View-Animation-视图动画"><a href="#View-Animation-视图动画" class="headerlink" title="View Animation(视图动画)"></a>View Animation(视图动画)</h3><ul>
<li>Tween Animation(补间动画)</li>
<li>Frame Animation(逐帧动画)</li>
</ul>
<h3 id="Property-Animation-属性动画"><a href="#Property-Animation-属性动画" class="headerlink" title="Property Animation(属性动画)"></a>Property Animation(属性动画)</h3><ul>
<li>ValueAnimator</li>
<li>ObjectAnimator</li>
</ul>
<h2 id="一-视图动画标签"><a href="#一-视图动画标签" class="headerlink" title="一. 视图动画标签"></a>一. 视图动画标签</h2><h3 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h3><p>Android动画由5种类型组成：alpha、scale、translate、rotate、set</p>
<h3 id="1-1配置XML动画文件"><a href="#1-1配置XML动画文件" class="headerlink" title="1.1配置XML动画文件"></a>1.1配置XML动画文件</h3><p>常用标签：</p>
<ul>
<li>alpha：渐变透明度动画</li>
<li>scale：渐变尺寸伸缩动画</li>
<li>translate：画面变换位置移动动画</li>
<li>rotate：画面转移旋转动画</li>
<li>set：定义动画集</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">scale</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromXScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toXScale</span>=<span class="string">&quot;1.4&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromYScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toYScale</span>=<span class="string">&quot;1.4&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span> = <span class="string">&quot;1000&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">scale</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-动画文件存放位置"><a href="#1-2-动画文件存放位置" class="headerlink" title="1.2 动画文件存放位置"></a>1.2 动画文件存放位置</h3><p>动画文件应该放于<code>res/anim</code>文件夹下，访问时使用<code>R.anim.XXX</code></p>
<h3 id="1-3-使用动画文件"><a href="#1-3-使用动画文件" class="headerlink" title="1.3 使用动画文件"></a>1.3 使用动画文件</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">animation</span> <span class="operator">=</span> AnimationUtils.loadAnimation(MainActivity.<span class="built_in">this</span>,R.anim.scale_down_anim);</span><br><span class="line">textView.startAnimation(animation);</span><br><span class="line"><span class="comment">//textView.clearAnimation();</span></span><br></pre></td></tr></table></figure>

<p>动画开始时，会将TextView的高度、宽度都从0缩放到1.4倍大小</p>
<h3 id="1-4-scale标签"><a href="#1-4-scale标签" class="headerlink" title="1.4 scale标签"></a>1.4 scale标签</h3><p><code>scale</code>标签用于缩放动画，该标签有如下几个属性</p>
<ul>
<li><code>android:fromXScale</code>：动画起始时，控件在X轴方向相对自身的缩放比例，浮点值，比如，1.0代表自身无变化，0.5代表缩放1倍，2.0代表放大1倍</li>
<li><code>android:toXScale</code>：动画结束时，控件在X轴相对自身的缩放比例，浮点值。</li>
<li><code>android:fromYScale</code>：动画起始时，控件在Y轴方向相对自身的缩放比例，浮点值。</li>
<li><code>android:toYScale</code>：动画结束时，控件在Y轴方向相对自身的缩放比例，浮点值。</li>
<li><code>android:pivotX</code>：缩放起始点X轴坐标，可以是数值、百分数、百分数p 三种样式，如50、50% 、50%p 。如果是数值， 表示在当前视图的左上角 ，即原点处加 50px，作为缩放起始点X轴坐标；如果是 50% 则表示在当前控件的左上角加上自己宽度的50%作为缩放起点X轴坐标；如果是50%p，则表示在当前父控件的左上角加上父控件宽度的50%作为缩放起始点X轴坐标。</li>
<li><code>android:pivotY</code>：缩放起始点X轴坐标，取值及含义与 android:pivotX相同。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">scale</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromXScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toXScale</span>=<span class="string">&quot;1.4&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromYScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toYScale</span>=<span class="string">&quot;1.4&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fillAfter</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;2000&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="1-5-Animation继承属性"><a href="#1-5-Animation继承属性" class="headerlink" title="1.5 Animation继承属性"></a>1.5 Animation继承属性</h3><p>所有的动画都继承<code>Animation</code>类</p>
<p>以 <code>scale</code>标签为例，讲述<code>Animation</code>类所具有的属性及其含义</p>
<ul>
<li><code>android:duration</code>：用于设置一次动画的持续时间，以毫秒为单位。</li>
<li><code>android:fillAfter</code>：如果设置为 true，则控件动画结束时，将保持动画结束时的状态。</li>
<li><code>android:fillBefore</code>：如果设置为 true，则控件动画结束时，将还原到初始化状态。</li>
<li><code>android fil!Enabed</code>：与android:fillBefore 效果相同，都是在控件动画结束时，将还原到初始化状态。</li>
<li><code>android:repeatCount</code>：用于指定动画的重复次数，当取值为<code>infinite</code> 时， 表示无限循环。</li>
<li><code>android:repeatMode</code>：用于设定重复的类型，有 reverse和restart两个值，其中reverse表示倒序回放： restart 表示重放，并且必须与- repeatCount 一起使用才能看到效果。</li>
<li><code>android:interpolator</code>：用于设定插值器，其实就是指定的动画效果，比如弹跳效果等。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">   <span class="attr">android:fillAfter</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">    &gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scale</span> <span class="attr">android:fromXScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toXScale</span>=<span class="string">&quot;0.4&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromYScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toYScale</span>=<span class="string">&quot;0.6&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:repeatMode</span>=<span class="string">&quot;reverse&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:repeatCount</span>=<span class="string">&quot;2&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:duration</span>=<span class="string">&quot;700&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-6-alpha标签"><a href="#1-6-alpha标签" class="headerlink" title="1.6 alpha标签"></a>1.6 alpha标签</h3><p>标签属性:</p>
<ul>
<li>android:fromAlpha: 动画开始时的透明度，取值范围为 0~ 1 .0, 0.0表示全透明， 1.0表示完全不透明。</li>
<li>android:toAlpha： 动画结束时的透明度</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;3000&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fillBefore</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">alpha</span> <span class="attr">android:fromAlpha</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toAlpha</span>=<span class="string">&quot;0.1&quot;</span></span></span><br><span class="line"><span class="tag">        /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-7-rotate标签"><a href="#1-7-rotate标签" class="headerlink" title="1.7 rotate标签"></a>1.7 rotate标签</h3><p><code>rotate</code>标签用于实现画面转移旋转动画效果，标签属性:</p>
<ul>
<li><code>android:fromDegress</code>：动画开始旋转时的角度位置，正值代表顺时针方向的度数，负值代表逆时针方向的度数</li>
<li><code>android:toDegress</code>：动画结束时旋转到的角度位 ，正值代表顺时针方向的度数，负值代表逆时针方向的度数</li>
<li><code>android:pivotX：</code>旋转中心点X轴坐标，默认旋转中心点是控件坐标原点。        可以是数值、百分数、 百分数p 三 种样式，比如 50 50% 50%p ，具体含义己在 scale 标签中讲述。</li>
<li><code>android:pivotY</code>：旋转中心点Y轴坐标。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">rotate</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;6000&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromDegrees</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toDegrees</span>=<span class="string">&quot;1080&quot;</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--    android:repeatCount=&quot;infinite&quot;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--    android:repeatMode=&quot;restart&quot;--&gt;</span></span><br><span class="line">		<span class="comment">&lt;!--    以中心点顺时针旋转3圈--&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-8-translate标签"><a href="#1-8-translate标签" class="headerlink" title="1.8 translate标签"></a>1.8 translate标签</h3><p><code>translate</code>标签用于实现画面变换位置移动动画效果。属性：</p>
<ul>
<li><code>android:fromXDelta</code>:起始点X轴坐标，可以是是数值、百分数、百分数p 三种样式。</li>
<li><code>android:fromYDelta</code>:起始点Y轴坐标</li>
<li><code>android:toXDelta</code>:终点X轴坐标</li>
<li><code>android:toYDelta</code>:终点Y轴坐标</li>
</ul>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fillAfter</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--控件从点(0,0)移动到(-80,-80)--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">translate</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:duration</span>=<span class="string">&quot;2000&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromXDelta</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromYDelta</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toXDelta</span>=<span class="string">&quot;-80&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toYDelta</span>=<span class="string">&quot;-80&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-9-set标签"><a href="#1-9-set标签" class="headerlink" title="1.9 set标签"></a>1.9 set标签</h3><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;3000&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fillAfter</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">alpha</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromAlpha</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toAlpha</span>=<span class="string">&quot;1.0&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scale</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromXScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromYScale</span>=<span class="string">&quot;0.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toXScale</span>=<span class="string">&quot;1.4&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toYScale</span>=<span class="string">&quot;1.4&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rotate</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromDegrees</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toDegrees</span>=<span class="string">&quot;720&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>控件完成了从小到大，旋转出场，透明度从0到1的组合效果。</p>
<p><em><strong>注意：在 set 标签中设直 repeateCount 属性是无效的，必须对每个动画单独设直才有作用</strong></em></p>
<h2 id="二、视图动画的代码实现"><a href="#二、视图动画的代码实现" class="headerlink" title="二、视图动画的代码实现"></a>二、视图动画的代码实现</h2><p>标签与所对应的类如下所示</p>
<table>
<thead>
<tr>
<th>scale</th>
<th>ScaleAnimation</th>
</tr>
</thead>
<tbody><tr>
<td>alpha</td>
<td>AlphaAnimation</td>
</tr>
<tr>
<td>rotate</td>
<td>RotateAnimation</td>
</tr>
<tr>
<td>translate</td>
<td>TranslateAnimation</td>
</tr>
<tr>
<td>set</td>
<td>AnimationSet</td>
</tr>
</tbody></table>
<h3 id="2-1-ScaleAnimation"><a href="#2-1-ScaleAnimation" class="headerlink" title="2.1 ScaleAnimation"></a>2.1 ScaleAnimation</h3><p>与1.4节XML对应的代码是：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">scaleAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ScaleAnimation</span>(</span><br><span class="line">           <span class="number">0</span>,<span class="number">1.4f</span>,<span class="number">0</span>,<span class="number">1.4f</span>,</span><br><span class="line">           Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,</span><br><span class="line">           Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line">scaleAnimation.setDuration(<span class="number">2000</span>);</span><br><span class="line">scaleAnimation.setFillAfter(<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>

<h3 id="2-2-AlphaAnimation"><a href="#2-2-AlphaAnimation" class="headerlink" title="2.2 AlphaAnimation"></a>2.2 AlphaAnimation</h3><p>与1.6节对应的代码是：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">alphaAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AlphaAnimation</span>(<span class="number">0.0f</span>,<span class="number">1.0f</span>);</span><br><span class="line">alphaAnimation.setDuration(<span class="number">3000</span>);</span><br><span class="line">alphaAnimation.setFillBefore(<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>

<h3 id="2-3-RotateAnimation"><a href="#2-3-RotateAnimation" class="headerlink" title="2.3 RotateAnimation"></a>2.3 RotateAnimation</h3><p>与1.7节对应的代码是：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">rotateAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">RotateAnimation</span>(<span class="number">0</span>,<span class="number">1080</span>,</span><br><span class="line">                Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line">rotateAnimation.setDuration(<span class="number">6000</span>);</span><br></pre></td></tr></table></figure>

<h3 id="2-4-TranslateAnimation"><a href="#2-4-TranslateAnimation" class="headerlink" title="2.4 TranslateAnimation"></a>2.4 TranslateAnimation</h3><p>与1.8节对应的代码是：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">translateAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TranslateAnimation</span>(<span class="number">0</span>,-<span class="number">80</span>,<span class="number">0</span>,-<span class="number">80</span>);</span><br><span class="line">translateAnimation.setDuration(<span class="number">2000</span>);</span><br></pre></td></tr></table></figure>

<h3 id="2-5-AnimationSet"><a href="#2-5-AnimationSet" class="headerlink" title="2.5 AnimationSet"></a>2.5 AnimationSet</h3><p>与1.9对应的代码是：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//创建AnimationSet对象</span></span><br><span class="line"><span class="type">AnimationSet</span> <span class="variable">animationSet</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AnimationSet</span>(<span class="literal">true</span>);</span><br><span class="line"><span class="comment">//创建动画对象</span></span><br><span class="line"><span class="type">Animation</span> <span class="variable">alphaAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AlphaAnimation</span>(<span class="number">0.0f</span>,<span class="number">1.0f</span>);</span><br><span class="line"><span class="type">Animation</span> <span class="variable">scaleAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ScaleAnimation</span>(<span class="number">0.0f</span>,<span class="number">1.4f</span>,<span class="number">0.0f</span>,<span class="number">1.4f</span>,</span><br><span class="line">          Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,</span><br><span class="line">          Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line"><span class="type">Animation</span> <span class="variable">rotateAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">RotateAnimation</span>(<span class="number">0</span>,<span class="number">720</span>,</span><br><span class="line">          Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,</span><br><span class="line">          Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line"><span class="comment">//添加动画到AnimationSet</span></span><br><span class="line">animationSet.addAnimation(alphaAnimation);</span><br><span class="line">animationSet.addAnimation(scaleAnimation);</span><br><span class="line">animationSet.addAnimation(rotateAnimation);</span><br><span class="line"><span class="comment">//设置属性</span></span><br><span class="line">animationSet.setDuration(<span class="number">3000</span>);</span><br><span class="line">animationSet.setFillAfter(<span class="literal">true</span>);</span><br><span class="line">binding.textView.startAnimation(animationSet);</span><br></pre></td></tr></table></figure>

<h3 id="2-7-Animation"><a href="#2-7-Animation" class="headerlink" title="2.7 Animation"></a>2.7 Animation</h3><p>其他函数</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">cancel</span><span class="params">()</span> <span class="comment">//取消动画</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">reset</span><span class="params">()</span>  <span class="comment">//将控件重置到动画开始前状态</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">setAnimationListener</span><span class="params">(Animation.AnimationListener listener)</span> <span class="comment">//设置动画监听</span></span><br></pre></td></tr></table></figure>

<p>示例：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">animationSet.setAnimationListener(<span class="keyword">new</span> <span class="title class_">Animation</span>.AnimationListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationStart</span><span class="params">(Animation animation)</span> &#123;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationEnd</span><span class="params">(Animation animation)</span> &#123;</span><br><span class="line">                animationSet.reset();</span><br><span class="line">                showTranslateAnimation(); <span class="comment">//展示移动动画 2.4节</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onAnimationRepeat</span><span class="params">(Animation animation)</span> &#123;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br></pre></td></tr></table></figure>

<h2 id="三、-插值器"><a href="#三、-插值器" class="headerlink" title="三、 插值器"></a>三、 插值器</h2><p>插值器是在 XML 中定义的动画修改器，它会影响动画的变化率。插值器可对现有的动画效果执行加速、减速、重复、退回等。</p>
<p>插值器通过 <code>android:interpolator</code> 属性应用于动画元素，该属性的值是对插值器资源的引用。</p>
<p>Android 中提供的所有插值器都是 <code>Interpolator</code> 类的子类。为便于您使用 <code>android:interpolator</code> 属性将插值器应用于动画，Android 针对每个插值器类包含了一个可供您引用的公共资源。下表指定了每个插值器要使用的资源：</p>
<table>
<thead>
<tr>
<th>插值器类</th>
<th>资源 ID</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/AccelerateDecelerateInterpolator?hl=zh-cn">AccelerateDecelerateInterpolator</a></td>
<td>@android:anim&#x2F;accelerate_decelerate_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/AccelerateInterpolator?hl=zh-cn">AccelerateInterpolator</a></td>
<td>@android:anim&#x2F;accelerate_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/AnticipateInterpolator?hl=zh-cn">AnticipateInterpolator</a></td>
<td>@android:anim&#x2F;anticipate_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/AnticipateOvershootInterpolator?hl=zh-cn">AnticipateOvershootInterpolator</a></td>
<td>@android:anim&#x2F;anticipate_overshoot_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/BounceInterpolator?hl=zh-cn">BounceInterpolator</a></td>
<td>@android:anim&#x2F;bounce_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/CycleInterpolator?hl=zh-cn">CycleInterpolator</a></td>
<td>@android:anim&#x2F;cycle_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/DecelerateInterpolator?hl=zh-cn">DecelerateInterpolator</a></td>
<td>@android:anim&#x2F;decelerate_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/LinearInterpolator?hl=zh-cn">LinearInterpolator</a></td>
<td>@android:anim&#x2F;linear_interpolator</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://developer.android.com/reference/android/view/animation/OvershootInterpolator?hl=zh-cn">OvershootInterpolator</a></td>
<td>@android:anim&#x2F;overshoot_interpolator</td>
</tr>
</tbody></table>
<p>您可以通过以下方式使用 <code>android:interpolator</code> 属性应用上述某个插值器：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">android:interpolator</span>=<span class="string">&quot;@android:anim/accelerate_interpolator&quot;</span>&gt;</span></span><br><span class="line">    ...</span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="自定义插值器"><a href="#自定义插值器" class="headerlink" title="自定义插值器"></a>自定义插值器</h3><p>如果您对平台提供的插值器（在上表中列出）不满意，则可以使用修改过的属性创建自定义插值器资源。例如，您可以调整 <code>AnticipateInterpolator</code> 的加速率或调整 <code>CycleInterpolator</code> 的循环次数。为此，您需要在 XML 文件中创建自己的插值器资源。</p>
<p>文件位置：</p>
<p><code>res/anim/*filename*.xml</code><br>该文件名将用作资源 ID。</p>
<p>编译后的资源数据类型：</p>
<p>指向相应插值器对象的资源指针。</p>
<p>资源引用：</p>
<p>在 XML 中：<code>@[*package*:]anim/*filename*</code></p>
<p>语法：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">InterpolatorName</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:attribute_name</span>=<span class="string">&quot;value&quot;</span></span></span><br><span class="line"><span class="tag">    /&gt;</span></span><br></pre></td></tr></table></figure>

<p>如果您不应用任何属性，则您的插值器的运作方式将与平台提供的插值器（在上表中列出）完全相同。</p>
<p>元素：</p>
<p>请注意，在 XML 中定义的每个 <code>Interpolator</code> 实现的名称都以小写字母开头。</p>
<ul>
<li><p><code>&lt;accelerateDecelerateInterpolator&gt;</code></p>
<p>变化率在开始和结束时缓慢，但在中间会加快。无属性。</p>
</li>
<li><p><code>&lt;accelerateInterpolator&gt;</code></p>
<p>变化率在开始时较为缓慢，然后会加快。属性：<code>android:factor</code>浮点数。加速率（默认为 1）。</p>
</li>
<li><p><code>&lt;anticipateInterpolator&gt;</code></p>
<p>先反向变化，然后再急速正向变化。属性：<code>android:tension</code>浮点数。要应用的张力（默认为 2）。</p>
</li>
<li><p><code>&lt;anticipateOvershootInterpolator&gt;</code></p>
<p>先反向变化，再急速正向变化并超过目标值，然后以最终值结束。属性：<code>android:tension</code>浮点数。要应用的张力（默认为 2）。<code>android:extraTension</code>浮点数。张力要乘以的倍数（默认值为 1.5）。</p>
</li>
<li><p><code>&lt;bounceInterpolator&gt;</code></p>
<p>变化会在结束时退回。无属性。</p>
</li>
<li><p><code>&lt;cycleInterpolator&gt;</code></p>
<p>按指定的循环次数重复动画。变化率符合正弦曲线图。属性：<code>android:cycles</code>整数。循环次数（默认值为 1）。</p>
</li>
<li><p><code>&lt;decelerateInterpolator&gt;</code></p>
<p>变化率开始时很快，然后减慢。属性：<code>android:factor</code>浮点数。减速率（默认值为 1）。</p>
</li>
<li><p><code>&lt;linearInterpolator&gt;</code></p>
<p>变化率恒定不变。无属性。</p>
</li>
<li><p><code>&lt;overshootInterpolator&gt;</code></p>
<p>先急速正向变化，再超过最终值，然后回到最终值。属性：<code>android:tension</code>浮点数。要应用的张力（默认为 2）。</p>
</li>
</ul>
<p>示例：</p>
<p>保存在 <code>res/anim/my_overshoot_interpolator.xml</code> 的 XML 文件：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">overshootInterpolator</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:tension</span>=<span class="string">&quot;7.0&quot;</span></span></span><br><span class="line"><span class="tag">    /&gt;</span></span><br></pre></td></tr></table></figure>

<p>此动画 XML 将应用插值器：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">scale</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:interpolator</span>=<span class="string">&quot;@anim/my_overshoot_interpolator&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromXScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toXScale</span>=<span class="string">&quot;3.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:fromYScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:toYScale</span>=<span class="string">&quot;3.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;700&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="四、动画示例"><a href="#四、动画示例" class="headerlink" title="四、动画示例"></a>四、动画示例</h2><h3 id="4-1-镜头由远及近效果"><a href="#4-1-镜头由远及近效果" class="headerlink" title="4.1 镜头由远及近效果"></a>4.1 镜头由远及近效果</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">ScaleAnimation</span> <span class="variable">scaleAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ScaleAnimation</span>(<span class="number">1f</span>,<span class="number">1.1f</span>,<span class="number">1f</span>,<span class="number">1.1f</span>,</span><br><span class="line">Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line">scaleAnimation.setDuration(<span class="number">3</span>*<span class="number">1000L</span>);</span><br><span class="line">scaleAnimation.setFillAfter(<span class="literal">true</span>);</span><br><span class="line">binding.imageView.setAnimation(scaleAnimation);</span><br><span class="line"></span><br><span class="line"><span class="type">AlphaAnimation</span> <span class="variable">alphaAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AlphaAnimation</span>(<span class="number">0.5f</span>,<span class="number">1.0f</span>);</span><br><span class="line">alphaAnimation.setDuration(<span class="number">3</span> * <span class="number">1000L</span>);</span><br><span class="line">alphaAnimation.setFillAfter(<span class="literal">true</span>);</span><br><span class="line">binding.ivSlogan.setAnimation(alphaAnimation);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">androidx.constraintlayout.widget.ConstraintLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">&quot;http://schemas.android.com/tools&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">&quot;.SplashActivity&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">ImageView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/imageView&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:background</span>=<span class="string">&quot;@drawable/bg_splash&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:contentDescription</span>=<span class="string">&quot;@string/app_name&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/slogan_bottom&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_marginBottom</span>=<span class="string">&quot;44dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">&quot;@string/slogan_txt&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:textColor</span>=<span class="string">&quot;@color/white&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:textSize</span>=<span class="string">&quot;18sp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintBottom_toBottomOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintEnd_toEndOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintStart_toStartOf</span>=<span class="string">&quot;parent&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">TextView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/slogan_top&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_marginBottom</span>=<span class="string">&quot;30dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">&quot;@string/slogan_top_text&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:textColor</span>=<span class="string">&quot;@color/white&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:textSize</span>=<span class="string">&quot;18sp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintBottom_toTopOf</span>=<span class="string">&quot;@+id/slogan_bottom&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintEnd_toEndOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintStart_toStartOf</span>=<span class="string">&quot;parent&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">ImageView</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">&quot;@+id/ivSlogan&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">&quot;wrap_content&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:layout_marginBottom</span>=<span class="string">&quot;80dp&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintBottom_toBottomOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintEnd_toEndOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintStart_toStartOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:layout_constraintTop_toTopOf</span>=<span class="string">&quot;parent&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">app:srcCompat</span>=<span class="string">&quot;@drawable/ic_logo_slogan&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">androidx.constraintlayout.widget.ConstraintLayout</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>动画效果</p>
<p><img src= "https://s2.loli.net/2023/03/09/dog2qQY83iTIvHM.gif" data-lazy-src="https://s2.loli.net/2023/03/28/eVJMkoiKzYaPqSN.gif" alt="动画效果"></p>
<h3 id="4-2-Loading动画效果"><a href="#4-2-Loading动画效果" class="headerlink" title="4.2 Loading动画效果"></a>4.2 Loading动画效果</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">RotateAnimation</span> <span class="variable">rotateAnimation</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">RotateAnimation</span>(<span class="number">0</span>,<span class="number">360f</span>,</span><br><span class="line">        Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>,Animation.RELATIVE_TO_SELF,<span class="number">0.5f</span>);</span><br><span class="line">rotateAnimation.setDuration(<span class="number">1500</span>);</span><br><span class="line">rotateAnimation.setRepeatCount(-<span class="number">1</span>);</span><br><span class="line">rotateAnimation.setRepeatMode(Animation.RESTART);</span><br><span class="line">rotateAnimation.setInterpolator(<span class="keyword">new</span> <span class="title class_">LinearInterpolator</span>());</span><br></pre></td></tr></table></figure>

<h3 id="4-3-扫描动画"><a href="#4-3-扫描动画" class="headerlink" title="4.3 扫描动画"></a>4.3 扫描动画</h3><p>动画文件</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">set</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:duration</span>=<span class="string">&quot;3000&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scale</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromXScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromYScale</span>=<span class="string">&quot;1.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:repeatCount</span>=<span class="string">&quot;infinite&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toXScale</span>=<span class="string">&quot;3.0&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toYScale</span>=<span class="string">&quot;3.0&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">alpha</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fromAlpha</span>=<span class="string">&quot;0.4&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:repeatCount</span>=<span class="string">&quot;infinite&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:toAlpha</span>=<span class="string">&quot;0.0&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animation</span> <span class="variable">animationSet1</span> <span class="operator">=</span> getScanAnimation();</span><br><span class="line"><span class="type">Animation</span> <span class="variable">animationSet2</span> <span class="operator">=</span> getScanAnimation();</span><br><span class="line">animationSet2.setStartOffset(<span class="number">750</span>);</span><br><span class="line"><span class="type">Animation</span> <span class="variable">animationSet3</span> <span class="operator">=</span> getScanAnimation();</span><br><span class="line">animationSet3.setStartOffset(<span class="number">1500</span>);</span><br><span class="line"><span class="type">Animation</span> <span class="variable">animationSet4</span> <span class="operator">=</span> getScanAnimation();</span><br><span class="line">animationSet4.setStartOffset(<span class="number">2250</span>);</span><br><span class="line"></span><br><span class="line">binding.startScan.setOnClickListener(view -&gt; &#123;</span><br><span class="line">    binding.circle1.startAnimation(animationSet1);</span><br><span class="line">    binding.circle2.startAnimation(animationSet2);</span><br><span class="line">    binding.circle3.startAnimation(animationSet3);</span><br><span class="line">    binding.circle4.startAnimation(animationSet4);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> Animation <span class="title function_">getScanAnimation</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> AnimationUtils.loadAnimation(<span class="built_in">this</span>,R.anim.scan_anim);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="五、逐帧动画"><a href="#五、逐帧动画" class="headerlink" title="五、逐帧动画"></a>五、逐帧动画</h2><p>示例：</p>
<p>在<code>res/Drawable</code>文件下新建动画文件:</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;utf-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">animation-list</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:oneshot</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/a1&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:duration</span>=<span class="string">&quot;80&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/a2&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:duration</span>=<span class="string">&quot;80&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/a3&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:duration</span>=<span class="string">&quot;80&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">animation-list</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在代码中使用：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">AnimationDrawable</span> <span class="variable">animationDrawable</span> <span class="operator">=</span> (AnimationDrawable) imageView.getBackground();</span><br><span class="line">animationDrawable.start();</span><br></pre></td></tr></table></figure>

<p><strong>android:oneshot 如果是false，表示无限循环，true表示只展示一次</strong></p>
<h3 id="5-1-AnimationDrawable类"><a href="#5-1-AnimationDrawable类" class="headerlink" title="5.1 AnimationDrawable类"></a>5.1 AnimationDrawable类</h3><p>在Android中，逐帧动画需要得到AnimationDrawable类的支持，它位于<code>android.graphics.drawable</code>包下，是<code>Drawable</code>的间接子类。它主要用来创建一个逐帧动画，并且可以对帧进行拉伸，把它设置为View的背景。</p>
<p>AnimationDrawable的常用函数：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">start</span><span class="params">()</span> <span class="comment">//开始播放逐帧动画</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">stop</span><span class="params">()</span> <span class="comment">//停止播放逐帧动画</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getDuration</span><span class="params">(<span class="type">int</span> i)</span> <span class="comment">//得到指定index帧的持续时间</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getNumberOfFrames</span><span class="params">()</span> <span class="comment">//得到当前AnimationDrawable所有的帧的数量</span></span><br><span class="line">Drawable <span class="title function_">getFrame</span><span class="params">(<span class="type">int</span> index)</span> <span class="comment">//得到指定index的帧对应的Drawable对象</span></span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isRunning</span><span class="params">()</span>	<span class="comment">//判断当前AnimationDrawable是否正在播放</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">setOneShot</span><span class="params">(<span class="type">boolean</span> oneShot)</span> <span class="comment">//设置AnimationDrawable是否执行一次，返回true，表示执行一次，返回false，表示无限循环</span></span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isOneShot</span><span class="params">()</span>	<span class="comment">//判断AnimationDrawable是否执行一次，返回true，表示执行一次，返回false，表示无限循环</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">addFrame</span><span class="params">(<span class="meta">@NonNull</span> Drawable frame, <span class="type">int</span> duration)</span> <span class="comment">//为AnimationDrawable添加一帧，并设置持续时间</span></span><br></pre></td></tr></table></figure>







</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://devsu.top">Carl Su</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://devsu.top/posts/d2fda48f/">https://devsu.top/posts/d2fda48f/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://devsu.top" target="_blank">码行</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Android/">Android</a></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2023/03/14/RLM1qPGF3S6sNnJ.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/f8577dac/" title="Android自定义View-03 属性动画"><img class="cover" src="https://s2.loli.net/2023/03/14/4qo6phTg3CRv71H.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Android自定义View-03 属性动画</div></div></a></div><div class="next-post pull-right"><a href="/posts/febb5061/" title="Android自定义View-01 绘图基础"><img class="cover" src="https://s2.loli.net/2023/03/14/GzPHlY2aEkeVO1M.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Android自定义View-01 绘图基础</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/be1a9334/" title="Android-在Activity中使用Menu"><img class="cover" src="https://img10.51tietu.net/pic/20191030/k2hqdmpfendk2hqdmpfend.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android-在Activity中使用Menu</div></div></a></div><div><a href="/posts/59a3d1d6/" title="ConstraintLayout-constraintDimensionRatio属性"><img class="cover" src="https://img0.baidu.com/it/u=2303708579,2686113994&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">ConstraintLayout-constraintDimensionRatio属性</div></div></a></div><div><a href="/posts/f305e41/" title="Android 11 分区存储"><img class="cover" src="https://img9.51tietu.net/pic/20190921/idxelngfttlidxelngfttl.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android 11 分区存储</div></div></a></div><div><a href="/posts/b79c82/" title="Android优秀开源第三方库"><img class="cover" src="https://img.qingwk.com/images/resource/global/2020/06/09/96961_46896196822_at.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android优秀开源第三方库</div></div></a></div><div><a href="/posts/aae49b39/" title="Hilt基本使用"><img class="cover" src="https://img1.baidu.com/it/u=1939248509,402303850&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-05</div><div class="title">Hilt基本使用</div></div></a></div><div><a href="/posts/6ed51752/" title="Android Studio 和Lombok插件不兼容问题"><img class="cover" src="https://img0.baidu.com/it/u=1087455262,471122708&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-05</div><div class="title">Android Studio 和Lombok插件不兼容问题</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E7%94%BB%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.</span> <span class="toc-text">动画类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#View-Animation-%E8%A7%86%E5%9B%BE%E5%8A%A8%E7%94%BB"><span class="toc-number">1.1.</span> <span class="toc-text">View Animation(视图动画)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Property-Animation-%E5%B1%9E%E6%80%A7%E5%8A%A8%E7%94%BB"><span class="toc-number">1.2.</span> <span class="toc-text">Property Animation(属性动画)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80-%E8%A7%86%E5%9B%BE%E5%8A%A8%E7%94%BB%E6%A0%87%E7%AD%BE"><span class="toc-number">2.</span> <span class="toc-text">一. 视图动画标签</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-number">2.1.</span> <span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1%E9%85%8D%E7%BD%AEXML%E5%8A%A8%E7%94%BB%E6%96%87%E4%BB%B6"><span class="toc-number">2.2.</span> <span class="toc-text">1.1配置XML动画文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E5%8A%A8%E7%94%BB%E6%96%87%E4%BB%B6%E5%AD%98%E6%94%BE%E4%BD%8D%E7%BD%AE"><span class="toc-number">2.3.</span> <span class="toc-text">1.2 动画文件存放位置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E4%BD%BF%E7%94%A8%E5%8A%A8%E7%94%BB%E6%96%87%E4%BB%B6"><span class="toc-number">2.4.</span> <span class="toc-text">1.3 使用动画文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-scale%E6%A0%87%E7%AD%BE"><span class="toc-number">2.5.</span> <span class="toc-text">1.4 scale标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-5-Animation%E7%BB%A7%E6%89%BF%E5%B1%9E%E6%80%A7"><span class="toc-number">2.6.</span> <span class="toc-text">1.5 Animation继承属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-6-alpha%E6%A0%87%E7%AD%BE"><span class="toc-number">2.7.</span> <span class="toc-text">1.6 alpha标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7-rotate%E6%A0%87%E7%AD%BE"><span class="toc-number">2.8.</span> <span class="toc-text">1.7 rotate标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-8-translate%E6%A0%87%E7%AD%BE"><span class="toc-number">2.9.</span> <span class="toc-text">1.8 translate标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-9-set%E6%A0%87%E7%AD%BE"><span class="toc-number">2.10.</span> <span class="toc-text">1.9 set标签</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%A7%86%E5%9B%BE%E5%8A%A8%E7%94%BB%E7%9A%84%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">3.</span> <span class="toc-text">二、视图动画的代码实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-ScaleAnimation"><span class="toc-number">3.1.</span> <span class="toc-text">2.1 ScaleAnimation</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-AlphaAnimation"><span class="toc-number">3.2.</span> <span class="toc-text">2.2 AlphaAnimation</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-RotateAnimation"><span class="toc-number">3.3.</span> <span class="toc-text">2.3 RotateAnimation</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-TranslateAnimation"><span class="toc-number">3.4.</span> <span class="toc-text">2.4 TranslateAnimation</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-AnimationSet"><span class="toc-number">3.5.</span> <span class="toc-text">2.5 AnimationSet</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-Animation"><span class="toc-number">3.6.</span> <span class="toc-text">2.7 Animation</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81-%E6%8F%92%E5%80%BC%E5%99%A8"><span class="toc-number">4.</span> <span class="toc-text">三、 插值器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8F%92%E5%80%BC%E5%99%A8"><span class="toc-number">4.1.</span> <span class="toc-text">自定义插值器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%8A%A8%E7%94%BB%E7%A4%BA%E4%BE%8B"><span class="toc-number">5.</span> <span class="toc-text">四、动画示例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E9%95%9C%E5%A4%B4%E7%94%B1%E8%BF%9C%E5%8F%8A%E8%BF%91%E6%95%88%E6%9E%9C"><span class="toc-number">5.1.</span> <span class="toc-text">4.1 镜头由远及近效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-Loading%E5%8A%A8%E7%94%BB%E6%95%88%E6%9E%9C"><span class="toc-number">5.2.</span> <span class="toc-text">4.2 Loading动画效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E6%89%AB%E6%8F%8F%E5%8A%A8%E7%94%BB"><span class="toc-number">5.3.</span> <span class="toc-text">4.3 扫描动画</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E9%80%90%E5%B8%A7%E5%8A%A8%E7%94%BB"><span class="toc-number">6.</span> <span class="toc-text">五、逐帧动画</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-AnimationDrawable%E7%B1%BB"><span class="toc-number">6.1.</span> <span class="toc-text">5.1 AnimationDrawable类</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By Carl Su</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">学无止境</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://devsu.top/posts/d2fda48f/'
    this.page.identifier = '/posts/d2fda48f/'
    this.page.title = 'Android自定义View-02 视图动画'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }

  document.getElementById('darkmode').addEventListener('click', () => {
    setTimeout(() => window.disqusReset(), 200)
  })
}

if ('Valine' === 'Disqus' || !true) {
  if (true) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>